// pages/preview/preview.js
var app = getApp();
const innerAudioContextRecord = wx.createInnerAudioContext();  //创建录音音频对象
const innerAudioContext = wx.createInnerAudioContext();  //创建音频对象
innerAudioContextRecord.autoplay = true;
innerAudioContext.loop = true; 
Page({

  /** 
   * 页面的初始数据
   */
  data: {
    position:'',
    musicStatus: false,  //音乐是否播放
    share:true,
    avatar:'', 
    nickName:'',
    month:'',
    day: '',
    greet: '',
    bgimg: '',
    iscompany:0,
  },
  tocominfo:function(e){
    wx.navigateTo({
      url: '/pages/company/company?cardid='+this.data.id,
    })
  },
  // musicHandle
  musicHandle(e) {
    let that = this;
    let musicStatus = that.data.musicStatus;
    if (musicStatus) {  
      innerAudioContext.pause();
      innerAudioContext.onPause(() => {
        console.log('暂停播放');
        that.setData({
          musicStatus: false
        })
      })
    } else {
      innerAudioContext.play();
      innerAudioContext.onPlay(() => {
        console.log('开始播放');
        that.setData({
          musicStatus: true
        })
      })
    }
  },
  // 播放音频
  audioCard: function(){
    let that = this;
    if(that.data.hasaudio){
      that.setData({
        musicStatus: false
      }, () => {
        innerAudioContext.stop()
        innerAudioContextRecord.play()
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this    
    var id = options.id;
    if(id){
      that.setData({
        id:id
      })
      wx.request({
        url: app.siteinfo.rootpath+'gethk_over',
        data:{id:id},
        success:function(res){
          if(res.data.code == 200){
            setTimeout(function () {
              that.getHeight()
            }, 10000);
            console.log(res.data.info);

            var content = res.data.info.zhufu.split("@#$");
            console.log(content)
            if (content[0]) {
              that.setData({
                greet: content,
              })
            } else {
              that.setData({
                greet: content[0],
              })
            }

            that.setData({
              avatar: res.data.info.logo,
              nickName: res.data.info.username,
              bgimg: res.data.info.bgimg,
              iscompany: res.data.info.iscompany,
            })
            innerAudioContext.src = res.data.info.music; // 设置音频地址
            innerAudioContextRecord.src = res.data.info.audio; // 设置音频地址
            innerAudioContext.onPlay(() => {
              console.log('开始播放')
            })
            innerAudioContext.onError((res) => {
              console.log(res.errMsg)
              console.log(res.errCode)
            })
            if (innerAudioContextRecord.src){
              that.setData({
                hasaudio:false
              })
              innerAudioContextRecord.onPlay(() => {
                console.log('录音开始播放')
              })
              innerAudioContextRecord.onError((res) => {
                console.log(res.errMsg)
                console.log(res.errCode)
              })
              innerAudioContextRecord.onEnded((res) => {
                console.log('录音播放结束')
                that.setData({
                  musicStatus: true
                }, () => { innerAudioContext.play() })
              })
            } else{
              that.setData({
                musicStatus: true,
                hasaudio:false
              }, () => { innerAudioContext.play() })
            }
            // 获取系统时间
            var myDate = new Date();
            let monthNew = myDate.getMonth()
            let dayNew = myDate.getDate()
            var month = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
            that.setData({
              month: month[monthNew],
              day: dayNew
            })
          }else{
            wx.switchTab({
              url: '/pages/index/index',
            })
          }
        }
      })
    }    
  },
  // 返回
  editCard: function () {
    
    innerAudioContext.stop()
    wx.switchTab({
      url: '/pages/index/index',
    })
    console.log(123)
  },
  // 发送朋友
  sendFriends: function () {
    if(!app.siteinfo.userinfo){
      wx.navigateTo({
        url: '/pages/welcome/welcome',
      })
      return;
    }
    innerAudioContext.stop()  
    let share = this.data.share
    this.setData({
      share:false
    }) 
  },
  // 点击阴影
  closeHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
  },
  actionSheetbindchange: function () {
    this.setData({
      actionSheetHidden: !this.data.actionSheetHidden
    })
  },
  // 生成卡片
  postersHandle: function () {
    let share = this.data.share
    this.setData({
      share: true
    })
    var that = this
    wx.navigateTo({
      url: '/pages/posters/posters?id=' + that.data.id,
    })
  },

  util: function (obj) {
    console.log(obj);
    console.log(-obj.list);
    var continueTime = (parseInt(obj.list / obj.container) + 1) * 8000;
    var setIntervalTime = 200 + continueTime;

    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });
    this.animation = animation;
    animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
    this.setData({
      animationData: animation.export()
    })
    setInterval(() => {
      animation.translateY(obj.container).step({ duration: 200, timingFunction: 'step-start' }).translateY(-obj.list + 10).step({ duration: continueTime });
      this.setData({
        animationData: animation.export()
      })
    }, setIntervalTime)
  },
  getHeight() {
    var obj = new Object();
    //创建节点选择器 
    var query = wx.createSelectorQuery();
    query.select('.greeting_wrap').boundingClientRect()
    query.select('.text').boundingClientRect()
    query.exec((res) => {
      obj.container = res[0].height; // 框的height
      obj.list = res[1].height; // list的height
      // return obj;
      console.log(obj.container)
      console.log(obj.list)
      this.util(obj);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    var position = wx.getStorageSync('position');
    that.setData({
      position: position
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.stop()
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.stop()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let share = this.data.share
    this.setData({
      share: true
    })

    var that = this
    wx.request({
      url: app.siteinfo.rootpath + 'reply',
      data: { id: that.data.id, uid: app.siteinfo.userinfo.id },
      success: function (res) {

      }
    })


    var title = app.siteinfo.userinfo.wechat_name+'给你送来了一份祝福';
    var path = '/pages/preview/preview?cardtype=common&id=' + this.data.id + '&shareid=' + app.siteinfo.userinfo.id;
    var bgimg = this.data.bgimg
    return {
      title: title,
      path: path,
      imageUrl: bgimg,
      success(e) {
        
      }
    }
    
  }
})